//
//  ExhibitHeadMoreViewController.m
//  ArtDog
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ExhibitHeadMoreViewController.h"
#import "BaseTableView.h"
#import "HLMacro.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"
#import "ExhibitTableViewCell.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"

#import "ExhibitMainModel.h"
#import "ExhibitArtist.h"
#import "ExhibitDetailsViewController.h"

@interface ExhibitHeadMoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *loadArray;

@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation ExhibitHeadMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self setupNavigationBar];
    [self createTableView];
    [self refreshTableView];
}

- (void)setupNavigationBar
{
    self.navigationItem.title = @"专题";
}

- (void)createTableView
{
    _tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"ExhibitTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"exhibitTableViewCell"];
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            ExhibitMainModel *exhibitModel = [ExhibitMainModel modelWithDic:dic];
            [_loadArray addObject:exhibitModel];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            ExhibitMainModel *exhibitModel = [ExhibitMainModel modelWithDic:dic];
            [_loadArray addObject:exhibitModel];
        }
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v3/disExhibitsAndInit"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"isRecommend": @"1", @"citySort": @"0", @"timeSort": @"0"}];
    
    return urlString;
}

#pragma mark - tableView 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _loadArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExhibitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"exhibitTableViewCell"];
    cell.exhibitModel = _loadArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115 + SCREEN_WIDTH / 3;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExhibitDetailsViewController *detailsVC = [[ExhibitDetailsViewController alloc]init];
    detailsVC.urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"exhibit", [NSString stringWithFormat:@"%@", [[_loadArray[indexPath.item]exhibitArtistList][0] idField]]] andParams:@{@"token": [NSString getUserDefaultToken]}];
    [self.navigationController pushViewController:detailsVC animated:YES];
}


#pragma mark - 刷新
- (void)refreshTableView
{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_tableView.mj_header mjRefreshStateHeaderStyle];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray.count % 10 == 0) {
            _pageIndex = _loadArray.count / 10 + 1;
        } else {
            _pageIndex = _loadArray.count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_tableView.mj_footer mjRefreshAutoStateFooterStyle];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
